package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.activity.MoneyIossunActivity;
import com.wenyu.wenyuyuedu.bean.personage.AccounMoueyBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.AccounMoneyServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*
* 钱包
* */
public class Wallet_Activity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.tv_wallet_detail)
    TextView tvWalletDetail;        //明细
    @InjectView(R.id.tv_walley_money)
    TextView tvWalleyMoney;         //提现
    @InjectView(R.id.tv_wallet_problem)
    TextView tvWalletProblem;       //常见问题
    @InjectView(R.id.tv_yue_money)
    TextView tvYueMoney;
    @InjectView(R.id.iv_left)
    ImageView ivLeft;
    private Intent intent;

    private Activity activity;
    private double i2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        StatusBarUtil.setStatusBar(Wallet_Activity.this, R.color.white);
        ButterKnife.inject(this);
        this.activity = Wallet_Activity.this;
        initControl();
    }

    /*展示余额*/
    private void requestData() {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        final AccounMoneyServer accounMoneyServer = ApiWrapper.getInstance().create(AccounMoneyServer.class);
        Call<AccounMoueyBean> accountMouey = accounMoneyServer.getAccountMouey(token);
        accountMouey.enqueue(new Callback<AccounMoueyBean>() {
            @Override
            public void onResponse(Call<AccounMoueyBean> call, Response<AccounMoueyBean> response) {
                AccounMoueyBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        int i = Integer.parseInt(body.getInfo());
                        i2 = (double) i / 100;
                        tvYueMoney.setText(i2 + "");
                    } else if (code == 0) {
                        showLoginDoalog();
                    }
                }
            }

            @Override
            public void onFailure(Call<AccounMoueyBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常", Toast.LENGTH_SHORT);
            }
        });
    }

    AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }

    /*
    * 初始化控件
    * */
    private void initControl() {
        intent = new Intent();
        tvWalletDetail.setOnClickListener(this);
        tvWalleyMoney.setOnClickListener(this);
        tvWalletProblem.setOnClickListener(this);
        ivLeft.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //明细
            case R.id.tv_wallet_detail:
                intent.setClass(Wallet_Activity.this, MoueyDetail.class);
                startActivity(intent);
                break;
            //提现
            case R.id.tv_walley_money:
                if (i2 >= 20) {
                    Intent intentEmbody = new Intent();
                    intentEmbody.setClass(Wallet_Activity.this, EmbodyActivity.class);
                    intentEmbody.putExtra("money", i2);
                    startActivity(intentEmbody);
                } else {
                    MyToast.showToast(Wallet_Activity.this, "最低提现金额20元", Toast.LENGTH_SHORT);
                }
                break;
            //常见问题
            case R.id.tv_wallet_problem:
                intent.setClass(Wallet_Activity.this,MoneyIossunActivity.class);
                startActivity(intent);
                break;
            /*返回*/
            case R.id.iv_left:
                finish();
                break;
        }
    }
}
